package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Course;
import com.service.CourseService;
import com.utils.ApiResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @description course
 * @date 2022-12-19
 */
@RestController
@RequestMapping(value = "/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 新增
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/insert")
    public ApiResult insert(@RequestBody Course course) {
        Object insert = courseService.insert(course);
        return new ApiResult(200, "新增成功", insert);
    }

    /**
     * 刪除
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/delete")
    public ApiResult delete(int id) {
        return new ApiResult(200, "删除成功", courseService.delete(id));
    }

    /**
     * 更新
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/update")
    public ApiResult update(@RequestBody Course course) {
        return new ApiResult(200, "更新成功", courseService.update(course));

    }


    /**
     * 查询 分页查询
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/pageList")
    public ApiResult pageList(@RequestParam(required = false, defaultValue = "1") int page,
                              @RequestParam(required = false, defaultValue = "5") int limit) {

        PageHelper.startPage(page, limit);
        List<Course> list = courseService.pageList(page, limit);
        // 取分页信息
        PageInfo<Course> pageInfo = new PageInfo<Course>(list);
        return new ApiResult(0, "success", (int) pageInfo.getTotal(), pageInfo.getList());
    }

}